/**********************************************************************************************
 GradientHolder
 Copyright (C) <2012> <John W. Buchanan>
 
 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 
 Except as contained in this notice, the name(s) of the above copyright holders shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ***********************************************************************************************/

#import "UIButtonWithGradients.h"


//************************************
// data for the gradients
//************************************
@implementation  GradientHolder: NSObject




@synthesize   selectedColors = m_SelectedColors;
@synthesize   disabledColors = m_DisabledColors;


@synthesize   layer = m_Layer;
@synthesize   startPoint = m_StartPoint;
@synthesize   endPoint = m_EndPoint;


-(void) updateFrame:(CGRect) frame
{
    float width, height, xoffset, yoffset;
    //**************************************
    // we are dealing with proportitons.
    // There is no error checking here.
    // If you need to do error checking it 
    // done in the setters if you set
    // GRAD_DEBUG (see the GradientHolder.h)
    // file
    //**************************************
    if (m_Relative) 
    {
		if (m_Width == 0.0f) 
		{
			width = frame.size.width;
		}
		else 
		{
			width = frame.size.width*m_Width;
		}
		
		if (m_Height == 0.0f) 
		{
			height = frame.size.width;
		}
		else 
		{
			height = frame.size.height*m_Height;
		}
       
        
        if (m_Xoffset < 0) 
        {
            xoffset = frame.size.width * (1+m_Xoffset);
        }
        else 
        {
            xoffset = frame.size.width * (m_Xoffset);
        }
        
        if (m_Yoffset < 0) 
        {
            yoffset = frame.size.height * (1+m_Yoffset);
        }
        else 
        {
            yoffset = frame.size.height * (m_Yoffset);            
        }
    }
    else // We are dealing with Pixel offsets not proportions.
    {
        if (m_Xoffset <0)
        {
            xoffset = frame.size.width + m_Xoffset;
        }
        else 
        {
            xoffset = m_Xoffset;
        }
        
        if (m_Yoffset <0)
        {
            yoffset = frame.size.height + m_Yoffset;
        }
        else 
        {
            yoffset = m_Yoffset;
        }
        
        if  (m_Width == 0.0f)
        {
            width = frame.size.width;
        }
        else 
        {
            width = m_Width;
        }
        if (m_Height == 0.0f)
        {
            height = frame.size.height;
        }
        else 
        {
            height = m_Height;
        }
        
        
    }
    m_Layer.frame = CGRectMake(xoffset,yoffset,width,height);
    if (m_Vertical) 
    {
        [m_Layer setStartPoint:CGPointMake(0.5f, 0.0f)];
        [m_Layer setEndPoint:CGPointMake(0.5f, 1.0f)];
    }
    else 
    {
        [m_Layer setStartPoint:CGPointMake(0.0f, 0.5f)];
        [m_Layer setEndPoint:CGPointMake(1.0f, 0.5f)];
    }
}






-(float) yOffset
{
    return m_Yoffset;
}
-(void) setYOffset:(float)yOffset 
{
    m_Yoffset = yOffset;
    [self updateFrame:m_ParentFrame];
}

-(float) height
{
    return m_Height;
}
-(void) setHeight:(float)height
{
    m_Height = height;
    [self updateFrame:m_ParentFrame];
}
-(float) xOffset
{
    return m_Xoffset;
}
-(void) setXOffset:(float)xOffset
{
    m_Xoffset = xOffset;
    [self updateFrame:m_ParentFrame];
}

-(float) width
{
    return m_Width;
}
-(void) setWidth:(float)width
{
    m_Width = width;
    [self updateFrame:m_ParentFrame];
}

-(BOOL) relative
{
    return m_Relative;
}
-(void) setRelative:(BOOL)relative
{
    m_Relative = relative;
    [self updateFrame:m_ParentFrame];
    
}
-(BOOL) vertical
{
    return m_Vertical;
}
-(void)setVertical:(BOOL)vertical
{
    m_Vertical = vertical;
    if (m_Vertical) 
    {
        m_StartPoint = CGPointMake(0.5f, 0.0f);
        m_EndPoint = CGPointMake(0.5f, 1.0f);
    }
    else 
    {
        m_StartPoint = CGPointMake(0.0f, 0.5f);
        m_EndPoint = CGPointMake(1.0f, 0.5f);
    }
    
    [self updateFrame:m_ParentFrame];
}

-(void) setNormalColors:(NSArray *)normalColors
{
	m_NormalColors = normalColors;
	[self updateFrame:m_ParentFrame];
}
-(NSArray *) normalColors
{
	return m_NormalColors;
}

-(id)initWithFrame:(CGRect)frame 
{
    self = [super init];
    if (self != nil) 
    {
        UIColor *high = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5];
        UIColor *low = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0];
		
		m_NormalColors = [NSArray arrayWithObjects:(id)[high CGColor], (id) [low CGColor], nil];
		
        high = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
        low = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
		
		m_SelectedColors = [NSArray arrayWithObjects:(id)[high CGColor], (id) [low CGColor], nil];
        
        high = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0];
        low = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0];
		
		m_DisabledColors = [NSArray arrayWithObjects:(id)[high CGColor], (id) [low CGColor], nil];
        
        
        m_Xoffset = 0.0f;
        m_Height = 0.5f;
        m_Yoffset = 0.0f;
        m_Width = 0.0f;
        
        m_Layer = [[CAGradientLayer alloc] init];
        [self makeNormal];        
        m_ParentFrame = frame;
        m_Vertical = YES;
        m_Relative = YES;
        [self updateFrame:m_ParentFrame];
        
    }
    return self;
}

-(void) makeNormal
{
	m_Layer.colors = m_NormalColors;
}
-(void) makeSelected
{
    m_Layer.colors = m_SelectedColors;
}
-(void) makeDisabled
{
    m_Layer.colors = m_DisabledColors;
}
@end

